#include "Effect.h"

#include <D3D10Effect.h>
#include <D3DCommon.h>
#include <D3DX10Async.h>
#include <dxerr.h>

#include <cstdint>

namespace D3D10Effects
{
    Effect::Effect(ID3D10Device * const device, const std::wstring& filename)
	    : mEffect(nullptr)
    {
	   uint32_t shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
		shaderFlags |= D3D10_SHADER_DEBUG;
		shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION;
#endif

		ID3D10Blob* compilationErrors = nullptr;
		HRESULT hr = 0;
		hr = D3DX10CreateEffectFromFile(filename.c_str(), 0, 0, 
			"fx_4_0", shaderFlags, 0, device, 0, 0, &mEffect, &compilationErrors, 0);
		if (FAILED(hr))
		{
			if (compilationErrors)
			{
				MessageBoxA(0, static_cast<char*> (compilationErrors->GetBufferPointer()), 0, 0);
				compilationErrors->Release();
			}
			DXTrace(__FILE__, (uint32_t)__LINE__, hr, L"D3DX10CreateEffectFromFile", true);
		} 
    }

    Effect::~Effect()
    {
	    mEffect->Release();
    }
}
